﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PositionalIndexer
{
    public class DocumentManager
    {
        public Dictionary<int, string> DocumentDictionary { get; set; }
        private Document tempDocument;

        public DocumentManager()
        {
            DocumentDictionary = new Dictionary<int, string>();
            tempDocument = new Document();
            tempDocument.Id = 1;
        }

        public void ParseDocument(StreamReader sr)
        {
            String line = String.Empty;
            while (sr.Peek() > 0)
            {
                line = sr.ReadLine() + "\n";
                FindDocuments(line);
            }
            sr.Close();
        }

        // <body ....>...</body>
        public void FindDocuments(String line)
        {
            if (tempDocument.IsBody)
            {
                tempDocument.Content += line;
                if (!tempDocument.IsBody)
                {
                    Document doc = (Document)tempDocument.Clone();
                    DocumentDictionary.Add(doc.Id, RemoveTag(doc.Content));
                    tempDocument.Initialize();
                }
            }
            else
            {
                tempDocument.Content = line;
            }
        }

        // remove html tag
        public string RemoveTag(string html)
        {
            return HtmlRemoval.StripTagsCharArray(html);
        }
    }
}
